//
//  ESortDetailViewController.m
//  TasteEnjoy
//
//  Created by XuRui on 16/4/21.
//  Copyright © 2016年 XuRui. All rights reserved.
//

#import "ESortDetailViewController.h"

@interface ESortDetailViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, retain) BaseTableView *tableView; /**< tableView */

@property (nonatomic, retain) NSMutableArray *arrData; /**< 数据 */
@property (nonatomic, assign) NSInteger page;/**< 用来保存请求新数据动态值 */

@end

@implementation ESortDetailViewController

- (void)dealloc
{
    _tableView.delegate = nil;
    _tableView.dataSource = nil;
    [_tableView release];
    [_arrData release];
    [_strID release];
    [_NaviTitle release];

    [super dealloc];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationItem.title = _NaviTitle;
    self.arrData = [NSMutableArray array];
    [self dataHandle];
    [self createTableView];
    [self refresh];
}

#pragma mark - ------------ data ---------------

- (void)dataHandle
{
    NSString *newContent = [NSString stringWithFormat:@"https://open.seriousapps.cn/3/enjoy_product/group_product_list.json?city_id=1&count=20&group_id=%@&lat=38.88336921532342&lng=121.5446045366585&page=0&sort=1", _strID ];
    
    NSString *new = [newContent stringByReplacingOccurrencesOfString:@"sort=1" withString:[NSString stringWithFormat:@"sort=%ld", _page]];
    [XrDataParse GETWithURL:new par:nil success:^(id responseObject) {
        
        if (_page == 1) {
            
            [_arrData removeAllObjects];
        }
        
        NSArray *arr = responseObject[@"content"];
        NSArray *arrModel  = [EDetailModel modelHandleWithArrar:arr];
        
        [_arrData addObjectsFromArray:arrModel];
        
        [self.tableView reloadData];
        
    } filed:^(NSError *error) {
        
        NSLog(@"enjoy分类数据加载失败");
    }];
}



#pragma mark - ------------ 上拉加载 下拉刷新 ---------------

- (void)refresh
{
    //刷新
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        _page = 1;
        [self dataHandle];
        [_tableView.mj_header endRefreshing];
    }];
    
    //加载
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        
       _page += 1;
        [self dataHandle];
        [_tableView.mj_footer endRefreshing];
    }];
    
    
}



#pragma mark - ------------ 创建Tableview ---------------
- (void)createTableView
{
    
    self.tableView = [[BaseTableView alloc]initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
    _tableView.dk_backgroundColorPicker = DKColorWithColors([UIColor whiteColor] ,[UIColor grayColor]);
    [self.view addSubview:_tableView];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [_tableView registerClass:[SortDetailTableViewCell class] forCellReuseIdentifier:@"pool"];
    [_tableView release];
}


#pragma mark - ------------ 返回个数 ---------------
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return _arrData.count;
}

#pragma mark - ------------ cell赋值 ---------------

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    SortDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"pool"];
    EDetailModel *m = _arrData[indexPath.row];
    
    cell.model = m;
    return cell;
    

}

#pragma mark - ------------ 返回高度 ---------------
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 150;
    
}


#pragma mark - ------------ 点击跳转 ---------------
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    ESortWebViewController *detail = [[ESortWebViewController alloc]init];
    
    CATransition *animation = [CATransition animation];
    animation.duration = 0.8f;
    [animation setType:@"rippleEffect"];
    [self.navigationController.view.layer addAnimation:animation forKey:nil];

    
    EDetailModel *m = _arrData[indexPath.row];
    detail.DetailID = m.product_id;
    [self.navigationController pushViewController:detail animated:YES];
    [detail release];

}



#pragma mark - ------------ 将要结束拖拽 当滑动速度>0 导航栏隐藏 <0出现 ---------------
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset
{
    
    if (velocity.y > 0) {
        self.navigationController.navigationBar.hidden = YES;
        self.tabBarController.tabBar.hidden = YES;
        
    }else{
        
        self.navigationController.navigationBar.hidden = NO;
        self.tabBarController.tabBar.hidden = NO;
        
    }
}

#pragma mark - ------------ scrollview 最后走的方法 滑动减速停止 导航栏出现 ---------------
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    
    self.navigationController.navigationBar.hidden = NO;
    self.tabBarController.tabBar.hidden = NO;
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
